package com.ruoyi.bl.service.impl;

import com.aliyun.bailian20231229.models.CreateMemoryResponse;
import com.aliyun.tea.TeaException;
import com.ruoyi.bl.domain.BlUser;
import com.ruoyi.bl.domain.MemoryId;
import com.ruoyi.bl.mapper.MemoryIdMapper;
import com.ruoyi.bl.service.MemoryIdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MemoryIdImpl implements MemoryIdService {
    @Autowired
    private MemoryIdMapper memoryIdMapper;
    /**
     * 添加memoryId
     *
     * @param memoryId
     * @throws Exception
     */
    @Override
    public void addMemoryId(MemoryId memoryId) throws Exception {
        int i = memoryIdMapper.addMemoryId(memoryId);
        System.out.println("添加memoryId成功");
    }

    @Override
    public MemoryId queryMemoryId(long id) throws Exception {
        MemoryId memoryId = memoryIdMapper.getMemoryId(id);
        return memoryId;
    }


    /**
     * 创建memoryID
     *
     * @return
     */
    @Override
    public String CreateMemoryId(BlUser blUser) throws Exception {
        com.aliyun.bailian20231229.Client client = createClient();
        com.aliyun.bailian20231229.models.CreateMemoryRequest createMemoryRequest = new com.aliyun.bailian20231229.models.CreateMemoryRequest()
                .setDescription("关于" + blUser.getUserName() + "的长期记忆体");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        String memoryId = "";
        try {
            // 复制代码运行请自行打印 API 的返回值
            CreateMemoryResponse memoryWithOptions = client.createMemoryWithOptions("llm-tt1zzffjohw0mige", createMemoryRequest, headers, runtime);
            System.out.println("生成的memoryid为" + memoryWithOptions.body.getMemoryId());
            memoryId = memoryWithOptions.body.getMemoryId();
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return memoryId;
    }


    public com.aliyun.bailian20231229.Client createClient() throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId("LTAI5tCVdykEfKy68Huo43qq")
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret("LUq5TPaynzWxYeQDlaIkoz1qgKa2Fs");
        // Endpoint 请参考 https://api.aliyun.com/product/bailian
        config.endpoint = "bailian.cn-beijing.aliyuncs.com";
        return new com.aliyun.bailian20231229.Client(config);
    }

}
